package cn.itcast.www.genericTest;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

public class GenericMethodTest {  
	  
    public static void main(String[] args) throws Exception {  
        Method[] methods = GenericMethodTest.class.getDeclaredMethods();  
        for (Method method : methods) {  
            System.out.println("method:" + method.getName());// 方法名  
  
            //获取泛型方法的泛型参数  
            System.out.println(" paramTypeType: ");  
            Type[] paramTypeList = method.getGenericParameterTypes();// 方法的参数列表  
            for (Type paramType : paramTypeList) {  
                System.out.println("  " + paramType);// 参数类型  
                if (paramType instanceof ParameterizedType)/* 如果是泛型类型 */{  
                    Type[] types = ((ParameterizedType) paramType).getActualTypeArguments();// 泛型类型列表  
                    System.out.println("  TypeArgument: ");  
                    for (Type type : types) {  
                        System.out.println("   " + type);  
                    }  
                }  
            }  
  
            //获取泛型方法的泛型返回值
            System.out.println(" returnType: ");  
            Type returnType = method.getGenericReturnType();// 返回类型  
            System.out.println("  " + returnType);  
            if (returnType instanceof ParameterizedType)/* 如果是泛型类型 */{  
                Type[] types = ((ParameterizedType) returnType).getActualTypeArguments();// 泛型类型列表  
                System.out.println("  TypeArgument: ");  
                for (Type type : types) {  
                    System.out.println("   " + type);  
                }  
            }  
  
        }  
  
    }  
  
    public static String method1(List list) {  
        return null;  
    }  
  
    private static Map<String, Double> method2(Map<String, Object> map) {  
        return null;  
    }  
  /** 执行结果：
method:main
 paramTypeType: 
  class [Ljava.lang.String;
 returnType: 
  void
method:method1
 paramTypeType: 
  interface java.util.List
 returnType: 
  class java.lang.String
method:method2
 paramTypeType: 
  java.util.Map<java.lang.String, java.lang.Object>
  TypeArgument: 
   class java.lang.String
   class java.lang.Object
 returnType: 
  java.util.Map<java.lang.String, java.lang.Double>
  TypeArgument: 
   class java.lang.String
   class java.lang.Double

   */
}  
